package Client_Module;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.Scanner;

public class Client_Caller {
    private Socket socket_client;
    public boolean force_stop = false;
    private Thread ccaller_read_thread;
    private Thread ccaller_send_thread;
    private boolean ifwork;
    private boolean pressure_test_mode = false;
    private Scanner input = new Scanner(System.in);
    private Random suiji = new Random(); // 生成随机数 用于随机过号

    public Client_Caller(String ip, int port) {
        try {
            ifwork = false;
            socket_client = new Socket(ip, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init() {
        System.out.print("请输入运行模式(1.手动 2.压力测试 3.退出系统):");
        switch (Integer.valueOf(input.nextLine())) {// 检测运行模式
            case 1:
                pressure_test_mode = false;
                break;
            case 2:
                pressure_test_mode = true;
                break;
            case 3:
                input.close();
                return;
        }
        System.out.println("开始运行");
        ifwork = false;
        Read();// 运行读取线程
        Send();// 运行发送线程
    }

    public String Get_response_WID(String text) {// 获取窗口号
        return text.substring(text.indexOf("窗口") + 2, text.indexOf("办理"));
    }

    public String Get_response_Num(String text) {// 获取号码
        return text.substring(text.indexOf("请") + 1, text.indexOf("号"));
    }

    public void Read() {
        ccaller_read_thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream indata = socket_client.getInputStream();
                    DataInputStream indatastream = new DataInputStream(indata);
                    // output("connect");
                    String temp;
                    // temp = indatastream.readUTF();
                    while (true) {
                        ifwork = false;
                        temp = indatastream.readUTF();
                        checkDown(temp);
                        if (pressure_test_mode == false) {// 下面运行手动模式
                            clear_control_board();
                            for (int i = 1; i <= 4; i++) {
                                if (ifwork == true && i != 1) {
                                    break;
                                }
                                if (ifwork == false && i == 4) {
                                    output("response#" + Get_response_WID(temp) + "$fail");// 发送过号请求
                                    break;
                                }

                                System.out.println();
                                System.out.println("Server:" + temp);
                                System.out.print("Manual_Mode 请输入窗口号:");

                                Thread.sleep(3000); // 默认3秒

                            }
                        } else {// 下面运行压力测试模式
                            System.out.println("Server:" + temp);
                            pressure_test(2000, temp); // 此代码用于压力测试 time设置延时
                        }

                        if (force_stop == true) {// 强行停止
                            break;
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        ccaller_read_thread.start();// 运行线程

    }

    public void Send() {
        ccaller_send_thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    String temp_input;
                    while (true) {
                        temp_input = input.nextLine();// 手动读取窗口号
                        output("response#" + temp_input + "$comein");// 发送进去窗口信息
                        ifwork = true;// 更新状态
                        if (force_stop == true) {// 强行停止
                            break;
                        }
                        // }

                    }

                } finally {
                    input.close();// 关闭
                }
            }
        });
        ccaller_send_thread.start();// 开启线程
    }

    public synchronized void output(String text) {// 发送数据
        // System.out.println(text);
        try {
            OutputStream outdata = socket_client.getOutputStream();
            DataOutputStream outdatastream = new DataOutputStream(outdata);
            outdatastream.writeUTF(text);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void pressure_test(int time, String text) {// 压力测试方法
        // System.out.println(text);
        if (pressure_test_mode == false) {
            return;
        }
        if ((suiji.nextDouble() * 10) <= 8) { // 用于随机过号
            output("response#" + Get_response_WID(text) + "$comein");
            System.out.println("Pressure_info:" + Get_response_Num(text) + "来了");
        } else {
            output("response#" + Get_response_WID(text) + "$fail");
            System.out.println("Pressure_info:" + Get_response_Num(text) + "过号");
        }
        ifwork = true;
        try {
            Thread.sleep(time);// 设置时间
        } catch (InterruptedException e) {
        }
    }

    private void clear_control_board() throws IOException {
        System.out.print("\033[H\033[2J");
        System.out.flush();
    }

    private void checkDown(String temp) {
        if (temp.equals("ShutdownNow")) {
            System.exit(0);
        }
    }
}
